#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix_diagnose
# REQUIRE: NETWORKING FILESYSTEMS ix-fstab
# KEYWORD: nojail

. /etc/rc.subr

name="ix_diagnose"
rcvar="ix_diagnose_enable"
start_cmd="ix_diagnose_start"
stop_cmd="ix_diagnose_stop"

ix_diagnose_start()
{
	local _pidfile
	local _seconds

	# add /usr/local/bin for python..
	PATH="$PATH:/usr/local/bin:/usr/local/sbin"
	export PATH
	_pidfile=`mktemp -t ix_diagnose`
	(
		sh -x /usr/local/bin/ixdiagnose -b \
			> /tmp/ixdiagnose_boot.log 2>&1 & 
		pid=$!
		echo $pid > $_pidfile;
		wait
		rm $_pidfile
	) &

	_seconds=5

	echo -n "Waiting up to $_seconds seconds for ixdiagnose to finish..."
	(
		while [ $_seconds -gt 0 ] ; do
			for i in 1 2 3 4 5 6 7 8 9 0 ; do
				if [ ! -e "$_pidfile" ] ; then
					echo " done."
					exit 1
				fi
				sleep 0.1
			done
			echo -n " $_seconds"
			_seconds=$(($_seconds - 1))
		done
		echo ". timed out.  Will proceed in the background."
	)
}

ix_diagnose_stop()
{
	# nothing
}

load_rc_config $name
run_rc_command "$1"

